import boto3
from termcolor import colored
import os
import sys

author = {
    "name":"gl4ssesbo1",
    "twitter":"https://twitter.com/gl4ssesbo1",
    "github":"https://github.com/gl4ssesbo1",
    "blog":"https://www.pepperclipp.com/"
}

needs_creds = True

variables = {
    "SERVICE": {
        "value": "s3",
        "required": "true",
        "description":"The service that will be used to run the module. It cannot be changed."
    },
    "BUCKET":{
        "value":"",
        "required":"true",
        "description":"The service that will be used to run the module. It cannot be changed."
    },
    "ACL":{
        "value":"private",
        "required":"true",
        "description":"The service that will be used to run the module. It cannot be changed."
    },
    "LOCATION":{
        "value":"us-east-1",
        "required":"true",
        "description":"The service that will be used to run the module. It cannot be changed."
    }
}

description = "Gets the name of a bucket or a list of buckets separated by comma (',') and a key file, key with wildcard ('*', 'key*', '*key' or 'k*y') and downloads that/those file/s. Files can also be some, separated by commas. Requires Secret Key and Access Key of an IAM that has access to it."

aws_command = "aws ec2 describe-instances --region {} --profile {}"

def exploit(profile, workspace):
    try:
        bucket = variables['BUCKET']['value']
        region = variables['LOCATION']['value']
        acl = variables['ACL']['value']
        response = profile.create_bucket(
            Bucket=bucket,
            CreateBucketConfiguration={
                'LocationConstraint':region,
            },
            ACL=acl
        )
        print(colored("[*] Bucket '{}' created with location: '{}'.".format(bucket, response['Location']), "green"))
    except profile.exceptions.BucketAlreadyExists:
        print(colored("Bucket '{}' already exists.".format(variables['BUCKET']['value']),"red"))

    except profile.exceptions.BucketAlreadyOwnedByYou:
        print(colored("Bucket '{}' already owned by you.".format(variables['BUCKET']['value']),"red"))

    except:
        e = sys.exc_info()[0]
        print(colored("[*] {}".format(e), "red"))